package com.formiaomiao.demain.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.util.MessageEvent;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.weight.LoadingDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdateFriendsRemarksActivity extends BaseActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.tv_class_submit)
    TextView tvClassSubmit;
    @BindView(R.id.et_friend_remarks)
    EditText etFriendRemarks;

    LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_friends_remarks);
        ButterKnife.bind(this);
        ViewInit();
    }

    private void ViewInit() {
        dialog = new LoadingDialog(this);
        tvClassSubmit.setVisibility(View.VISIBLE);
        tvClassName.setText("设置备注");
        tvClassSubmit.setText("完成");
        if (getIntent().getStringExtra("friendremarks") == null
                || getIntent().getStringExtra("friendremarks").equals("")) {
            etFriendRemarks.setHint(getIntent().getStringExtra("username"));
        } else {
            etFriendRemarks.setHint(getIntent().getStringExtra("friendremarks"));
        }
    }

    private void UpdateRemarksInit(String name) {
        dialog.show();
        HttpParams params = new HttpParams();
        params.put("friendfrom", StaticValues.userid);
        params.put("friendto", getIntent().getIntExtra("id", 0));
        params.put("friendremarks", name);
        OkGo.<String>post(BaseUrl.UPDATEREMARKS).
                headers("Authorization", PreferenceUtils.getString(this, PreferenceValues.TOKEN))
                .params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (response.body().contains("成功")) {
                    EventBus.getDefault().post(new MessageEvent("备注:"+etFriendRemarks.getText().toString()));
                    finish();
                } else {
                    ToastUtils.show(UpdateFriendsRemarksActivity.this, "修改失败");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                dialog.dismiss();
            }
        });
    }

    @OnClick({R.id.iv_class_back, R.id.tv_class_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.tv_class_submit:
                if (etFriendRemarks.getText().toString().isEmpty()) {
                    UpdateRemarksInit(getIntent().getStringExtra("nickname"));
                } else {
                    UpdateRemarksInit(etFriendRemarks.getText().toString());
                }
                break;
        }
    }
}
